package com.guoguo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Purview;
import com.guoguo.entity.TaskInfo;
import com.guoguo.mapper.TaskInfoMapper;
import com.guoguo.service.TaskInfoService;
import com.guoguo.utils.StringUtils;
import com.guoguo.utils.UUIDUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TaskInfoServiceImpl implements TaskInfoService {

    @Autowired
    private TaskInfoMapper taskInfoMapper;

    @Override
    public PageBean<TaskInfo> getTaskInfoList(Map<String, Object> param) {
        int pageNumber = param.get("pageNumber") == null ? PaginationContext.getPageNum() : Integer.parseInt(String.valueOf(param.get("pageNumber")));
        int pageSize = param.get("pageSize") == null ? PaginationContext.getPageSize() : Integer.parseInt(String.valueOf(param.get("pageSize")));
        PageHelper.startPage(pageNumber, pageSize);
        return new PageBean<TaskInfo>(taskInfoMapper.getTaskInfoList(param));
    }

    @Override
    public TaskInfo findTaskInfo(TaskInfo taskInfo) {
        return taskInfoMapper.findTaskInfo(taskInfo);
    }

    @Override
    public int saveOrUpdateTaskInfo(TaskInfo taskInfo) {
        int num = 0;
        if (StringUtils.isEmptyString(taskInfo.getId())) {
            taskInfo.setId(UUIDUtils.getUUID());
            taskInfo.setRequireCount(taskInfo.getRequireCount() == null ? 0 : taskInfo.getRequireCount());
            taskInfo.setPrizeCount1(taskInfo.getPrizeType1() == null ? 0 : taskInfo.getPrizeType1());
            taskInfo.setPrizeCount2(taskInfo.getPrizeType2() == null ? 0 : taskInfo.getPrizeType2());
            taskInfo.setPrizeCount3(taskInfo.getPrizeType3() == null ? 0 : taskInfo.getPrizeType3());
            taskInfo.setCreateTime(new Date());
            taskInfo.setStatus(taskInfo.getStatus() == null ? 1 : taskInfo.getStatus());
            taskInfo.setTaskDesc(taskInfo.getTaskDesc() + " ：{0}/{1}");
            num = taskInfoMapper.saveTaskInfo(taskInfo);
        } else {
            num = taskInfoMapper.updateTaskInfo(taskInfo);
        }
        return num;
    }

    @Override
    public int updateBatch(String ids, int status) {
        Map<String, Object> param = new HashedMap();
        List<String> idsList = Arrays.asList(ids.split(","));
        param.put("ids", idsList);
        param.put("status", status);
        int num = taskInfoMapper.updateBatch(param);
        return num;
    }

    @Override
    public int removeTaskInfo(TaskInfo taskInfo) {
        int num = taskInfoMapper.removeTaskInfo(taskInfo);
        return num;
    }
}
